import VConsole from 'vconsole';

let vConsoleInstance = null;

/**
 * 初始化 VConsole
 * 只有当 URL 参数中包含 debug=true 时才会显示
 */
export const initVConsole = () => {
  console.log('=== 开始初始化 VConsole ===');
  
  // 检查 URL 参数中是否包含 debug=true
  const urlParams = new URLSearchParams(window.location.search);
  const debugParam = urlParams.get('debug');
  
  console.log('URL 参数 debug:', debugParam);
  
  // 如果 debug=true，则初始化 VConsole
  if (debugParam === 'true') {
    console.log('检测到 debug=true，初始化 VConsole');
    
    if (!vConsoleInstance) {
      vConsoleInstance = new VConsole({
        defaultPlugins: ['system', 'network', 'element', 'storage'],
        maxLogNumber: 1000,
        onReady: () => {
          console.log('VConsole 已准备就绪');
        }
      });
      
      console.log('VConsole 实例已创建');
    } else {
      console.log('VConsole 实例已存在，跳过创建');
    }
  } else {
    console.log('未检测到 debug=true，不初始化 VConsole');
  }
};

/**
 * 销毁 VConsole 实例
 */
export const destroyVConsole = () => {
  if (vConsoleInstance) {
    console.log('销毁 VConsole 实例');
    vConsoleInstance.destroy();
    vConsoleInstance = null;
  }
};

/**
 * 获取 VConsole 实例
 */
export const getVConsoleInstance = () => {
  return vConsoleInstance;
};
